package com.xhs.controller;



import com.xhs.bo.LoginCodeBo;
import com.xhs.bo.UserBo;
import com.xhs.bo.UserFindBo;
import com.xhs.config.SystemConfig;
import com.xhs.service.intf.UserService;
import com.xhs.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("api/user")
@Api(value = "用户操作接口",tags = "用户的注册、登录、找回")
public class UserController {

    @Autowired
    private UserService service;

    //注册
    @PostMapping("register")
    @ApiOperation(value ="注册",notes = "注册")
    public R regex(@RequestBody UserBo bo, HttpServletRequest request){
        return service.register(bo,request.getRemoteAddr());
    }

    //校验手机号是否存在
    @GetMapping("checkphone")
    @ApiOperation(value ="校验手机号",notes = "校验手机号")
    public R chencePhone(String phone){
        return service.checkPhone(phone);
    }

    //账号密码登陆
    @PostMapping("passlogin")
    @ApiOperation(value ="账号密码登陆",notes = "账号密码登陆")
    public R passLogin(@RequestBody UserBo bo,HttpServletRequest request){
        return service.loginByPassword(bo,request.getRemoteAddr());
    }

    //账号验证码登陆
    @PostMapping("codelogin")
    @ApiOperation(value ="账号验证码登陆",notes = "账号验证码登陆")
    public R codeLogin(@RequestBody LoginCodeBo bo, HttpServletRequest request){
        return service.loginCode(bo,request.getRemoteAddr());
    }

    //找回密码-校验验证码
    @PostMapping("checkfindcode")
    @ApiOperation(value ="找回密码-校验验证码",notes = "找回密码-校验验证码")
    public R checkFindCode(@RequestBody UserFindBo bo, HttpServletRequest request){
        return service.checkFindCode(bo,request.getRemoteAddr());
    }

    //找回密码-修改密码
    @PostMapping("updatepassword")
    @ApiOperation(value ="找回密码-修改密码",notes = "找回密码-修改密码")
    public R updatePassword(@RequestBody UserBo bo,HttpServletRequest request){
        return service.updatePassword(bo,request.getRemoteAddr());
    }

    //校验令牌
    @GetMapping("checktoken")
    @ApiOperation(value ="校验令牌",notes = "校验令牌")
    public R checkToken(HttpServletRequest request){
        return service.checkToken(request.getHeader(SystemConfig.HEADER_TOKEN));
    }

    //通过令牌获取用户信息
    @GetMapping("getuser")
    @ApiOperation(value ="通过令牌获取用户信息",notes = "通过令牌获取用户信息")
    public R getUserByToken(HttpServletRequest request){
        return service.queryUser(request.getHeader(SystemConfig.HEADER_TOKEN));
    }

    //注销令牌
    @GetMapping("deltoken")
    @ApiOperation(value ="注销令牌",notes = "注销令牌")
    public R delToken(HttpServletRequest request){
        return service.deleToken(request.getHeader(SystemConfig.HEADER_TOKEN));
    }
}
